package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.service.AdminBrandService;
import com.cskaoyan.market.service.AdminBrandServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;


@WebServlet("/admin/brand/*")
public class AdminBrandController extends HttpServlet {

        AdminBrandService adminBrandService=new AdminBrandServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();

        String op = requestURI.replace(req.getContextPath() + "/admin/brand/", "");
        if("create".equals(op)) {
            System.out.println("create in starting...");
            create(req,resp);
        }

        if("update".equals(op)) {
            update(req,resp);
        }

        if("delete".equals(op)) {
            delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requesBody = req.getReader().readLine();
        String id= JacksonUtil.parseString(requesBody, "id");

        adminBrandService.delete(id);

        Object ok = ResponseUtil.ok();
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody= req.getReader().readLine();
//        ObjectMapper objectMapper = new ObjectMapper();
//        MarketBrand marketBrand = objectMapper.readValue(s, MarketBrand.class);
//        System.out.println(marketBrand.toString());


        String desc = JacksonUtil.parseString(requestBody, "desc");
        String flooPrice = JacksonUtil.parseString(requestBody, "flooPrice");
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String picUrl = JacksonUtil.parseString(requestBody, "picUrl");
        String name = JacksonUtil.parseString(requestBody, "name");
        MarketBrand marketBrand=new MarketBrand();
        marketBrand.setId(id);
        marketBrand.setDesc(desc);
        marketBrand.setUpdateTime(LocalDateTime.now());
        marketBrand.setDeleted(false);

        MarketBrand resualUpdate = adminBrandService.update(marketBrand);

        Object ok = ResponseUtil.ok(resualUpdate);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();

        String op = requestURI.replace(req.getContextPath() + "/admin/brand/", "");
        if("list".equals(op)) {
            list(req,resp);
        }


    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String desc = JacksonUtil.parseString(requestBody, "desc");
        String floorPrice = JacksonUtil.parseString(requestBody, "floorPrice");
        String name = JacksonUtil.parseString(requestBody, "name");
        String picUrl = JacksonUtil.parseString(requestBody, "picUrl");

        MarketBrand resultMarketBrand = adminBrandService.create(desc, floorPrice, name, picUrl);
//        Map<String,String> data = new HashMap<>();
//        data.put("addTime",resultMarketBrand.getAddTime().toString());
//        data.put("desc",resultMarketBrand.getDesc());
//        data.put("floorPrice",resultMarketBrand.getFloorPrice().toString());
//        data.put("id",resultMarketBrand.getId().toString());
//        data.
        Object ok = ResponseUtil.ok(resultMarketBrand);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("list starting!!!");

        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketBrand> brandList = adminBrandService.list(page, limit, id, name, sort, order);
        Object o = ResponseUtil.okList(brandList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }
}
